package co.edu.udistrital.lucene;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import weka.classifiers.bayes.NaiveBayes;
import weka.core.Instances;
import weka.core.SerializationHelper;

/**
 * This code was originally written for Erik's Lucene intro java.net article
 */
public class IndexerNewDocuments {

	
	public static void main(String[] args) throws Exception {
		IndexerNewDocuments indexer = new IndexerNewDocuments();
		indexer.clasifyWeka();
		System.out.println("Finalizo");
	}

	public void clasifyWeka() {
		try {
			 NaiveBayes naybe2 = (NaiveBayes) SerializationHelper.read("D:\\naybe2.model");
			Instances unlabeled = new Instances(
                    new BufferedReader(
                      new FileReader("D:\\Nuevos.arff")));
			unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
			Instances labeled = new Instances(unlabeled);
			for (int i = 0; i < unlabeled.numInstances() ; i++) {
				   double clsLabel = naybe2.classifyInstance(unlabeled.instance(i));
				   labeled.instance(i).setClassValue(clsLabel);
			}
			 // save labeled data
			 BufferedWriter writer = new BufferedWriter(
			                           new FileWriter("D:\\NuevosClasificados.arff"));
			 writer.write(labeled.toString());
			 writer.newLine();
			 writer.flush();
			 writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}